package com.zzyk.microCloud.api.controller;

import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.model.pojo.Result;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.AlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/alarm")
public class ApiAlarmController extends ApiBaseController {

    @Autowired
    private AlarmInfoService alarmInfoService;


    /**
     * 历史试图 告警设备类型
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getHistoryAlarmDeviceType")
    public Message getHistoryAlarmDeviceType(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.selectHistoryAlarmDeviceType(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList())).addData("count", alarmInfoService.getCountHistoryAlarmDeviceType(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 历史视图 告警原因分布
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmCauseDistribution")
    public Message getAlarmCauseDistribution(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.selectAlarmCauseDistribution(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }
    /**
     * 统计传入时间的今天和昨天每个小时的报警数量
     *
     * @param dateStr
     * @return
     */
    @GetMapping("/getCountAlarmsByHour")
    public Message getCountAlarmsByHour(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.findCountAlarmsByHour(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 查询某一天的各个告警状态的数量
     *
     * @return
     */
    @GetMapping("/countToDayAlarmNumber")
    public Message countToDayAlarmNumber(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.countToDayAlarmNumber(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 查询告警原因分布
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmNumberGroupByContent")
    public Message getAlarmNumberGroupByContent(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.findAlarmNumberGroupByContent(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 根据设备类型分组统计各个类型报警设备数量
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmDeviceType")
    public Message getAlarmDeviceType(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success()
                .addData("data", alarmInfoService.selectAlarmDeviceType(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList())).addData("count", alarmInfoService.getAlarmCountBy(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 历史试图 告警处理信息
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmHandlingInformation")
    public Message getAlarmHandlingInformation(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.selectAlarmHandlingInformation(dateStr, sysUser.getEnterprise(), sysUser.getDeviceList()));
    }

    /**
     * 告警管理历史视图里告警统计
     *
     * @param dateStr
     * @return
     */
    @GetMapping("/getAlarmStatistics")
    public Message getAlarmStatistics(String dateStr) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.selectAlarmStatistics(dateStr, sysUser.getEnterprise(), sysUser.getDeviceList()));
    }

    /**
     * 获取当月告警信息 当月每天的告警数量
     *
     * @return
     */
    @GetMapping("/getToMonthAlarmData")
    public Message getToMonthAlarmData() throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return Message.success().addData("data", alarmInfoService.selectToMonthAlarmData(sysUser.getEnterprise(), sysUser.getDeviceList()));
    }


    @GetMapping("/findAlarmByParamsTop10")
    public Result findAlarmByParamsTop10(Long dId, String beginTime, String endTime, Long status) throws UserException {

        SysUser userInfo = getUser().getSysUser();
        return Result.ok(alarmInfoService.selectAlarmByParamsTop10(userInfo.getEnterprise().longValue(), dId, beginTime, endTime, status));

    }

    @GetMapping("/findAlarmByParamsTo")
    public Message findAlarmByParamsTo(Long dId, String beginTime, String endTime, Long status, Integer pageIndex, Integer PageSize) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        return alarmInfoService.selectAlarmByParams(sysUser.getEnterprise().longValue(), dId, beginTime, endTime, status, null, pageIndex, PageSize, sysUser.getDeviceList());

    }

    @GetMapping("/findAlarmByParams")
    public Result findAlarmByParams(Long dId, String beginTime, String endTime, Long status) throws UserException {
        SysUser sysUser = getUser().getSysUser();
        try {
            return Result.ok(alarmInfoService.apiSelectAlarmByParams(sysUser.getEnterprise().longValue(), dId, beginTime, endTime, status, null, sysUser.getDeviceList()));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

}
